package Com.HupochuanAdmin.Dal.Tab;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.DBManager.DBManager;
import Com.Hupochuan.Vo.MovieSource;

public class MovieSourceTab {
	/*
	 * 本类私有对象
	 */
	private DBManager<MovieSource> dbManager = new DBManager<MovieSource>(MovieSource.class);
	
	/*
	 * 单一实例
	 */
	private MovieSourceTab(){}
	private static MovieSourceTab movieSourceTab = new MovieSourceTab();
	public static MovieSourceTab getObject(){
		return movieSourceTab;
	}
	
	/*
	 * 添加数据
	 */
	public int appendData(long movieId,long typeId,long languageId,long subtitleId,long modeId,String url,float weight){		
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" insert into MovieSource set ");
		sqlBuff.append(" 	movieId=${movieId}, ");
		sqlBuff.append(" 	typeId=${typeId}, ");
		sqlBuff.append(" 	languageId=${languageId}, ");
		sqlBuff.append(" 	subtitleId=${subtitleId}, ");
		sqlBuff.append(" 	modeId=${modeId}, ");
		sqlBuff.append(" 	url=${url}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	createTime=now(), ");
		sqlBuff.append(" 	updateTime=now() ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		paraHash.put("typeId",typeId);
		paraHash.put("languageId",languageId);
		paraHash.put("subtitleId",subtitleId);
		paraHash.put("modeId",modeId);
		paraHash.put("url",url);
		paraHash.put("weight",weight);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 更新资源数据
	 */
	public int updateData(long typeId,long languageId,long subtitleId,long modeId,String url,float weight,long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" update MovieSource set ");
		sqlBuff.append(" 	typeId=${typeId}, ");
		sqlBuff.append(" 	languageId=${languageId}, ");
		sqlBuff.append(" 	subtitleId=${subtitleId}, ");
		sqlBuff.append(" 	modeId=${modeId}, ");
		sqlBuff.append(" 	url=${url}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	updateTime=now() ");
		sqlBuff.append(" where id=${id} and ( ");
		sqlBuff.append(" 	typeId!=${typeId} ");
		sqlBuff.append(" 	or languageId!=${languageId} ");
		sqlBuff.append(" 	or subtitleId!=${subtitleId} ");
		sqlBuff.append(" 	or modeId!=${modeId} ");
		sqlBuff.append(" 	or url!=${url} ");
		sqlBuff.append(" 	or weight!=${weight} ");
		sqlBuff.append(" ) ");
		
		//sql参数部分
		paraHash.put("typeId",typeId);
		paraHash.put("languageId",languageId);
		paraHash.put("subtitleId",subtitleId);
		paraHash.put("modeId",modeId);
		paraHash.put("url",url);
		paraHash.put("weight",weight);
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 删除数据
	 */
	public int deleteData(long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieSource where id=${id} ");
		
		//sql参数部分
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据电影ID 删除数据
	 */
	public int deleteDataByMovieId(long movieId){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieSource where movieId=${movieId} ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 获得电影资源列表
	 */
	public List<MovieSource> findMovieSourceList(long movieId){
		//参数部分
		List<MovieSource> movieSourceList = new Vector<MovieSource>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select MovieSource.* , ");
		sqlBuff.append(" 	SourceType.name as typeName, ");
		sqlBuff.append(" 	SourceType.logoPositionY as typeLogoPositionY, ");
		sqlBuff.append(" 	SourceType.searchUrl as typeSearchUrl, ");
		sqlBuff.append(" 	SourceType.adDuration as typeAdDuration, ");
		sqlBuff.append(" 	SourceLanguage.name as languageName, ");
		sqlBuff.append(" 	SourceSubtitle.name as subtitleName, ");
		sqlBuff.append(" 	SourceMode.name as modeName ");
		sqlBuff.append(" from MovieSource ");
		sqlBuff.append(" left join SourceType on MovieSource.typeId=SourceType.id ");
		sqlBuff.append(" left join SourceLanguage on MovieSource.languageId=SourceLanguage.id ");
		sqlBuff.append(" left join SourceSubtitle on MovieSource.subtitleId=SourceSubtitle.id ");
		sqlBuff.append(" left join SourceMode on MovieSource.modeId=SourceMode.id ");
		sqlBuff.append(" where MovieSource.movieId=${movieId} ");
		sqlBuff.append(" order by ");
		sqlBuff.append(" 	MovieSource.weight desc, ");
		sqlBuff.append(" 	SourceType.weight desc, ");
		sqlBuff.append(" 	SourceType.id, ");
		sqlBuff.append(" 	MovieSource.id ");

		//sql参数部分
		paraHash.put("movieId",movieId);
		
		//执行sql 
		movieSourceList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(),paraHash);
		
		//返回列表
		return movieSourceList;
	}
}
